<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '房间评价'"
    :close-on-click-modal="false"
    :before-close="closeHandle"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户昵称" prop="nickname">
      <el-input v-model="dataForm.nickname" placeholder="用户昵称"></el-input>
    </el-form-item>
    <el-form-item label="房间类型" prop="roomtypename">
        <el-popover
          ref="typeListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            :data="typeList"
            :props="typeListTreeProps"
            node-key="id"
            ref="typeListTree"
            @current-change="typeListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.roomtypename" v-popover:typeListPopover :readonly="true" placeholder="点击选择类别" class="menu-list__input"></el-input>
    </el-form-item>
    <el-form-item label="评价内容" prop="contents">
      <textarea style="width: 100%;height: 140px" v-model="dataForm.contents"></textarea>
    </el-form-item>
      <el-form-item label="图片" prop="imgurl">
        <el-upload
          accept="image/jpeg,image/gif,image/png,image/bmp"
          :action="url"
          :before-upload="beforeUploadHandle"
          :on-success="successHandle"
          :on-remove="handleRemove"
          :multiple="true"
          :file-list="fileList"
          :limit="8"
          list-type="picture-card">
          <i class="el-icon-plus"></i>
        </el-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :disabled="subable" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
  import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        subable: false,
        disable: true,
        visible: false,
        url: '',
        num: 0,
        successNum: 0,
        fileList: [],
        filename: '',
        dataForm: {
          id: 0,
          customerid: '',
          typename: '',
          nickname: '',
          roomtypeid: '',
          roomtypename: '',
          receiptid: '',
          contents: '',
          imgurl: '',
          replyinfo: '',
          replytime: '',
          commenttime: ''
        },
        dataRule: {
          nickname: [
            { required: true, message: '用户昵称不能为空', trigger: 'blur' }
          ],
          contents: [
            { required: true, message: '评价内容不能为空', trigger: 'blur' }
          ],
          roomtypename: [
            { required: true, message: '房间类型不能为空', trigger: 'blur' }
          ]
        },
        typeList: [],
        typeListTreeProps: {
          label: 'typename',
          children: 'children'
        }
      }
    },
    methods: {
      init: function (id) {
        this.url = this.$http.adornUrl(`/comment/comment/upload?token=${this.$cookie.get('token')}`)
        this.dataForm.id = id || 0
        this.$http({
          url: this.$http.adornUrl('/roomtype/roomtype/selectlist'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.typeList = treeDataTranslate(data.typeList, 'id')
        }).then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.subable = false
            this.$refs['dataForm'].resetFields()
          })
        }).then(() => {
          if (!this.dataForm.id) {
            // 新增
            this.typeListTreeSetCurrentNode()
          } else {
            // 修改
            this.$http({
              url: this.$http.adornUrl(`/comment/comment/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.customerid = data.comment.customerid
                this.dataForm.nickname = data.comment.nickname
                this.dataForm.roomtypename = data.comment.roomtypename
                this.dataForm.roomtypeid = data.comment.roomtypeid
                this.dataForm.receiptid = data.comment.receiptid
                this.dataForm.contents = data.comment.contents
                this.fileList = eval(data.comment.imgurl)
                this.dataForm.replyinfo = data.comment.replyinfo
                this.dataForm.replytime = data.comment.replytime
                this.dataForm.commenttime = data.comment.commenttime
                this.typeListTreeSetCurrentNode()
              }
            })
          }
        })
      },

      typeListTreeCurrentChangeHandle (data, node) {
        this.dataForm.roomtypeid = data.id
        this.dataForm.roomtypename = data.typename
      },
      // 菜单树设置当前选中节点
      typeListTreeSetCurrentNode () {
        this.$refs.typeListTree.setCurrentKey(this.dataForm.roomtypeid)
        this.dataForm.roomtypename = (this.$refs.typeListTree.getCurrentNode() || {})['typename']
      },
      // 上传之前
      beforeUploadHandle (file) {
        if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
          this.$message.error('只支持jpg、png、gif格式的图片！')
          return false
        }
        this.num++
      },
      // 上传成功
      successHandle (response, file, fileList) {
        this.successNum++
        if (response && response.code === 0) {
          if (this.num === this.successNum) {
            this.fileList = fileList
          }
        } else {
          this.$message.error(response.msg)
        }
      },
      handleRemove (file, fileList) {
        this.filename = ''
        if (file.response !== undefined) {
          this.filename = file.response.name
        } else {
          this.filename = file.name
        }
        this.$http({
          url: this.$http.adornUrl(`/comment/comment/picdelete`),
          method: 'post',
          data: this.$http.adornData({
            name: this.filename
          })
        })
        this.fileList = fileList
      },
    // 弹窗关闭时
      closeHandle () {
        this.fileList = []
        this.$emit('refreshDataList')
        this.visible = false
      },
      // 表单提交
      dataFormSubmit () {
        this.subable = true
        this.dataForm.imgurl = ''
        for (var i = 0; i < this.fileList.length; i++) {
          if (this.fileList[i].response !== undefined) {
            this.fileList[i].name = this.fileList[i].response.name
          }
          this.dataForm.imgurl += this.fileList[i].name + ','
        }
        if (this.dataForm.imgurl !== '') {
          this.dataForm.imgurl = this.dataForm.imgurl.substr(0, this.dataForm.imgurl.length - 1)
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/comment/comment/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',

              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'customername': this.dataForm.customername,
                'nickname': this.dataForm.nickname,
                'roomtypename': this.dataForm.roomtypename,
                'roomtypeid': this.dataForm.roomtypeid,
                'receiptid': this.dataForm.receiptid,
                'contents': this.dataForm.contents,
                'imgurl': this.dataForm.imgurl,
                'replyinfo': this.dataForm.replyinfo,
                'replytime': this.dataForm.replytime,
                'commenttime': this.dataForm.commenttime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.disable = false
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1000,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          } else {
            this.subable = false
          }
        })
      }
    }
  }
</script>
